package com.mds.group.four.test;

import android.app.Activity;
import android.database.Cursor;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.Toast;

public class AppList extends Activity {
	private AppDAO dao;
	String[] FileName = null;
	boolean mIsStart;
	int j = 0;
	String Path = Environment.getExternalStorageDirectory().getAbsolutePath() + "//TestApp//";
	
	ImageView iv;
	EditText et;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.play);
		if(dao == null) dao = new AppDAO(this);
		Cursor c = dao.selectNotes();
		//if (c != null) startManagingCursor(c);
		
		int count = c.getCount();		
		
		if(count == 0){
			et = (EditText)findViewById(R.id.playtext);
			et.setText("목록이 없습니다.");
		}else{
			FileName = new String[count];
			Log.i("----", "count: " + count);
			c.moveToFirst();
			int i = 0;
			do {
				FileName[i] = c.getString(1).toString();
				Log.i("----", "title: " + FileName[i]);
				i++;
			} while(c.moveToNext());
			iv = (ImageView)findViewById(R.id.playImage);
			et = (EditText)findViewById(R.id.playtext);
			
			iv.setBackground(Drawable.createFromPath(Path + "img_" +FileName[j]));
			/*Matrix matrix = new Matrix();
			matrix.postRotate(90);
			iv.setImageMatrix(matrix);*/
			iv.setScaleType(ImageView.ScaleType.FIT_XY);
			
			/* EditText를 이용해서 화면에 출력할 문자 설정. */
			et.setText(FileName[j]);
		}
		
		
		// 음성 재생.
		findViewById(R.id.playImage).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				String aud_path = Path + "aud_" + FileName[j];
				if (aud_path.length() == 0 || mIsStart) {
					Toast.makeText(AppList.this, "녹음을 먼저 하세요.", 0).show();
					return;
				}
				MediaPlayer player = new MediaPlayer();
				try {
					player.setDataSource(aud_path);
					player.prepare();
					player.start();
				} catch (Exception e) {
					Toast.makeText(AppList.this, "error : " + e.getMessage(), 0).show();
				}
			}
		});
		
		// 다음 버튼 눌렀을때..
		findViewById(R.id.nextBt).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//Log.i("----", "파일네임렝스 : " + FileName.length);
				j++;
				if(j == FileName.length){
					Toast.makeText(AppList.this, "마지막입니다.", Toast.LENGTH_SHORT).show();
				}else {
					iv = (ImageView)findViewById(R.id.playImage);
					et = (EditText)findViewById(R.id.playtext);
					iv.setBackground(Drawable.createFromPath(Path + "img_" +FileName[j]));
					/*Matrix matrix = new Matrix();
					matrix.postRotate(90);
					iv.setImageMatrix(matrix);*/
					iv.setScaleType(ImageView.ScaleType.FIT_XY);
					et.setText(FileName[j]);
				}
			}
		});
		
		// 이전 버튼 눌렀을때..
		findViewById(R.id.prevBt).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if(j > 0) {
					j--;
					iv = (ImageView)findViewById(R.id.playImage);
					et = (EditText)findViewById(R.id.playtext);
					iv.setBackground(Drawable.createFromPath(Path + "img_" +FileName[j]));
					/*Matrix matrix = new Matrix();
					matrix.postRotate(90);
					iv.setImageMatrix(matrix);*/
					iv.setScaleType(ImageView.ScaleType.FIT_XY);
					et.setText(FileName[j]);
				}
				Toast.makeText(AppList.this, "처음입니다.", Toast.LENGTH_SHORT).show();
			}
		});
	}
}
